package com.zaofan.controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.GoodsOrderDetail;
import com.zaofan.bean.GoodsOrderInfo;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.GoodsOrderDetailService;
import com.zaofan.service.GoodsOrderInfoService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/goods/order")
public class GoodsOrderController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private GoodsOrderInfoService orderInfoService = new GoodsOrderInfoService();
	private GoodsOrderDetailService orderDetailService = new GoodsOrderDetailService();

	/**
	 * 全部订单列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<GoodsOrderInfo> orders = orderInfoService.findAllOrder(params);
			Long count = orderInfoService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", orders, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 今天下单成功订单
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void today_list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			Calendar now = Calendar.getInstance();
			params.put("orderTime", now.get(Calendar.YEAR) + "-" + (formatNum(now.get(Calendar.MONTH) + 1)) + "-"
					+ formatNum(now.get(Calendar.DATE)));
			params.put("payState", "in2,3");
			List<GoodsOrderInfo> orders = orderInfoService.findAll(params);
			Long count = orderInfoService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", orders, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			GoodsOrderInfo rank = orderInfoService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", rank, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void detail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			GoodsOrderInfo orderInfo = orderInfoService.findById(id);
			if (orderInfo == null) {
				JsonUtils.writeListFailJson("查询成功!!", response);
				return;
			}
			List<GoodsOrderDetail> details = orderDetailService.findByOrderId(id);
			JsonUtils.writeSuccessJson("查询成功", details, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("查询成功!!", response);
	}

	public String formatNum(int num) {
		if (num > 9) {
			return num + "";
		} else {
			return "0" + num + "";
		}

	}

	protected void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String orderNo = request.getParameter("orderNo");
			orderInfoService.payOrderByAdminMoney(orderNo);
			JsonUtils.writeSuccessJson("支付成功!!", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("支付失败", response);
		}
		JsonUtils.writeFailJson("支付失败!!", response);
	}

}
